package com.sxyc.tintin.model;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.plugin.activerecord.Model;

/**
 * @Description: 添加车辆信息
 * @author mark 2017年2月27日 下午5:53:37
 */
public class CarInfo extends Model<CarInfo> {

	private static final long serialVersionUID = -4937918453309860757L;
	public static final CarInfo dao = new CarInfo();

	// 使用jFinal 日志
	private com.jfinal.log.Log log = com.jfinal.log.Log.getLog(this.getClass());

	public boolean saveCarInfo(CarInfo carInfoInfo) {
		boolean isSuccess = false;
		try {
			if (carInfoInfo != null) {
				// 保存carInfo信息
				CarInfo carInfo = getCarInfo(carInfoInfo.get("DeviceId"));
				if (carInfo != null) {
					// 如果以下字段有变更则更新，否则忽略
					if (isModify(carInfo, carInfoInfo)) {
						carInfo.set("CarNo", carInfoInfo.get("CarNo"));   //车牌号
						carInfo.set("FrameNo", carInfoInfo.get("FrameNo")); //车架号
						carInfo.set("EngineNo", carInfoInfo.get("EngineNo")); //发动机号
						carInfo.set("Province",carInfoInfo.get("Province")); //省份
						carInfo.set("City",carInfoInfo.get("City"));        //城市
						carInfo.set("UpdatedBy", "System");
						carInfo.set("UpdatedDate", new Date());
						isSuccess = carInfo.update();
						if (isSuccess) {
							log.info("更新车辆信息 [ " + carInfo.toString()
									+ " ] 成功！");
						} else {
							log.info("更新车辆信息 [ " + carInfo.toString()
									+ " ] 失败！");
						}
					} else {
						isSuccess = true;
						log.info("忽略更新车辆信息 [ " + carInfo.toString() + " ] ！");
					}
					
				} else {
					carInfo = new CarInfo();
					carInfo.set("CarNo", carInfoInfo.get("CarNo"));
					carInfo.set("FrameNo", carInfoInfo.get("FrameNo"));
					carInfo.set("EngineNo", carInfoInfo.get("EngineNo"));
					carInfo.set("Province",carInfoInfo.get("Province")); 
					carInfo.set("City", carInfoInfo.get("City"));
					carInfo.set("DeviceId", carInfoInfo.get("DeviceId"));
					carInfo.set("Status", 1);
					carInfo.set("CreatedBy", "System");
					carInfo.set("CreatedDate", new Date());
					carInfo.set("UpdatedBy", "System");
					carInfo.set("UpdatedDate", new Date());
					isSuccess = carInfo.save();
					if (isSuccess) {
						log.info("新增车辆信息 [ " + carInfo.toString() + " ] 成功！");
					} else {
						log.info("新增车辆信息 [ " + carInfo.toString() + " ] 失败！");
					}
				}
			} else {
				log.info("车辆信息 为空，忽略保存！");
			}
		} catch (Exception e) {
			log.error("保存车辆信息异常，原因：" + e.getMessage());
		}
		return isSuccess;
	}

	public boolean isModify(CarInfo oldCarInfo, CarInfo newCarInfo) {
		boolean isModify = false;
		try {

			if (oldCarInfo == null && newCarInfo != null) {
				isModify = true;
			}
			if (oldCarInfo != null && newCarInfo == null) {
				isModify = true;
			}
			if (oldCarInfo != null && newCarInfo != null) {
				if (!StringUtils.equals(oldCarInfo.getStr("CarNo"), newCarInfo.getStr("CarNo"))
						|| !StringUtils.equals(oldCarInfo.getStr("FrameNo"), newCarInfo.getStr("FrameNo"))
						|| !StringUtils.equals(oldCarInfo.getStr("EngineNo"), newCarInfo.getStr("EngineNo"))
						|| !StringUtils.equals(oldCarInfo.getStr("City"), newCarInfo.getStr("City"))
						|| !StringUtils.equals(oldCarInfo.getStr("DeviceId"), newCarInfo.getStr("DeviceId"))
						|| !StringUtils.equals(oldCarInfo.getStr("Remark"), newCarInfo.getStr("Remark"))) {
					isModify = true;
				}
			}

		} catch (Exception e) {
			log.error("判断车辆信息是否有更新异常，原因：" + e.getMessage());
		}
		return isModify;
	}
	
	
	public CarInfo getCarInfo(Object object) {
		return CarInfo.dao.findFirst("select * from carinfo where DeviceId = ? ",
					new Object[] { object });
	}
	
}
